# -*-coding:utf-8 -*-
from django.http import HttpResponse, Http404
from forfood.address.models import *
from forfood.helpers.user_helper import *
none = "None"
def get_province(request):
    
    provinces = Province.objects.all()
    rv = "province&<option></option>"
    for p in provinces:
        if p.province == none:
            continue
        rv = rv + "<option value='%d'>%s</option>"%(p.id,p.province)
    
    return HttpResponse(rv)

def get_city(request,pid):
    rv = "city&<option></option>"
    
    try:
        province = Province.objects.get(id = pid)
    except:
        return HttpResponse(rv)
    
    city = City.objects.filter(province=province)    
    for c in city:
        if c.city == none:
            continue
        rv = rv + "<option value='%d'>%s</option>"%(c.id,c.city)
    
    return HttpResponse(rv)

def get_zone(request,cid):
    rv = "zone&<option></option>"
    
    try:
        city = City.objects.get(id = cid)
    except:
        return HttpResponse(rv)
    
    zone = Zone.objects.filter(city=city)    
    for z in zone:
        if z.zone == none:
            continue
        rv = rv + "<option value='%d'>%s</option>"%(z.id,z.zone)
        
    return HttpResponse(rv)

def get_zone_detail(request,zid):
    rv = "zone_detail&<option></option>"
    
    try:
        zone = Zone.objects.filter(id = zid)
    except:
        return HttpResponse(rv)
    
    zone_detail = ZoneDetail.objects.filter(zone=zone)    
    for zd in zone_detail:
        if zd.zone_detail == none:
            continue
        rv = rv + "<option value='%d'>%s</option>"%(zd.id,zd.zone_detail)
        
    return HttpResponse(rv)
def address_submit(request,zdid,more):
    customer = find_customer(request.user)
    if not customer:
        return HttpResponse("User not find")
    try:
        zone_detail = ZoneDetail.objects.get(id = zdid)
    except:
        return HttpResponse("Address error")
    try:
        address = Address.objects.get(zone_detail=zone_detail, more=more)
    except:
        address = Address.objects.create(zone_detail=zone_detail, more=more)
    customer.address = address
    customer.save()
        
    return HttpResponse(customer.get_address())